﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Pex.Framework;


namespace Benchmarks.McMinnThesisBen
{
    [TestClass]
    public class TelephoneNumber
    {
        static int LINE_FEED_CHAR = 12;
        static int ZERO_CHAR = 48;
        static int ONE_CHAR = 49;
        static int TWO_CHAR = 50;
        static int THREE_CHAR = 51;
        static int FOUR_CHAR = 52;
        static int FIVE_CHAR = 53;
        static int SIX_CHAR = 54;
        static int SEVEN_CHAR = 55;
        static int EIGHT_CHAR = 56;
        static int NINE_CHAR = 57;

        int position;
        bool local;
        bool international;
        bool national;

        bool valid;
        bool error;

        public TelephoneNumber()
        {
            position = 0;
            local = false;
            international = false;
            national = false;

            valid = false;
            error = false;
        }

        public void validate_uk_tel_no(int unicode_char)
        {
            valid = false;
            error = false;
            if (unicode_char == LINE_FEED_CHAR)
            {
                if (local && position == 7)
                {
                    valid = true;
                }
                else if (national && position == 11)
                {
                    valid = true;
                }
                else if (international && position == 14)
                {
                    valid = true;
                }
                else
                {
                    error = true;
                }
            }
            else if (unicode_char >= ZERO_CHAR && unicode_char <= NINE_CHAR)
            {
                if (position == 0)
                {
                    if (unicode_char == ZERO_CHAR)
                    {
                        national = true;
                    }
                    else
                    {
                        local = true;
                    }
                }
                if (position == 1 && unicode_char == ZERO_CHAR && national)
                {
                    international = true;
                    national = true;
                }
                if ((position == 2 || position == 3) &&
                unicode_char != FOUR_CHAR && international)
                {
                    error = true;
                }
                if (position == 4 && international && unicode_char == ZERO_CHAR)
                {
                    error = true;
                }
                position++;
            }
            else
            {
                error = true;
            }
            if (error)
            {
                position = 0;
                international = false;
                local = false;
                national = false;
            }
        }

        /// <summary>
        /// creates a telephone number
        /// </summary>
        /// <returns></returns>
        public static TelephoneNumber Create1()
        {
            TelephoneNumber tn = new TelephoneNumber();
            tn.validate_uk_tel_no(10);
            return tn;
        }

        //run with C:\pex\bin\Debug>Pex Benchmarks.dll /mf:TestTelephoneNumberFitness
        // adding /fte outputs all generated tests
        //[PexMethod(MaxRuns = 100, MaxConditions = int.MaxValue, MaxRunsWithoutNewTests = int.MaxValue)]        
        //public void TestTelephoneNumberFitness(int[] elements)
        //{
        //    TestTelephoneNumber(elements);
        //}

        //run with C:\pex\bin\Debug>Pex Benchmarks.dll /mf:TestTelephoneNumberDefault
        // adding /fte outputs all generated tests
        //[PexMethod(MaxRuns = 100, MaxConditions = int.MaxValue, MaxRunsWithoutNewTests = int.MaxValue)]
        //public void TestTelephoneNumberDefault(int[] elements)
        //{
        //    TestTelephoneNumber(elements);
        //}

        //private void TestTelephoneNumber(int[] elements)
        //{
        //    PexAssume.IsTrue(elements != null);

        //    TelephoneNumber c = new TelephoneNumber();
        //    for (int i = 0; i <= (elements.Length - 1); i++)
        //    {
        //        c.validate_uk_tel_no(elements[i]);
        //    }
        //}
    }
}
